const util = require('../../utils/util.js');
const api = require('../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nameShow:true,
    shareShow:true,
    name:"",
    share:"",
    canEdit:false,
    userInfo:{},
    teacherId:undefined,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if(options.from){
    //   this.setData({
    //     canEdit:true
    //   })
    // }
    if(options.teacherId){
      this.setData({
        teacherId: options.teacherId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getTeacherInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  updateName(){
    this.setData({
      nameShow: !this.data.nameShow,
      name:this.data.userInfo.name
    })
  },

  updateShare(){
    this.setData({
      shareShow: !this.data.shareShow,
      share:this.data.userInfo.share
    })
  },

  bindinputName(e){
    this.setData({
      name: e.detail.value
    });
  },

  bindinputShare(e){
    this.setData({
      share: e.detail.value
    });
  },

  bindconfirmName(e){
    let that = this;
    util.request(api.UpdateFullName, {
      userId: wx.getStorageSync('userInfo').id,
      fullName: that.data.name
    }, 'POST').then(function (res) {
      if (res.code == 200) {
        that.setData({
          nameShow:true,
        })
        that.getTeacherInfo()
      } else {
        wx.showToast({
          title: '姓名修改失败',
          icon: 'none'
        });
      }
    })
  },

  bindconfirmShare(e){
    let that = this;
    util.request(api.UpdateSelfShow, {
      userId: wx.getStorageSync('userInfo').id,
      selfShow: that.data.share
    }, 'POST').then(function (res) {
      if (res.code == 200) {
        that.setData({
          shareShow: true,
        })
        that.getTeacherInfo()
      } else {
        wx.showToast({
          title: '个人分享修改失败',
          icon: 'none'
        });
      }
    })
  },

  getTeacherInfo(){
    let that = this;
    let userId = wx.getStorageSync('userInfo').id;
    let teacherId = this.data.teacherId ? this.data.teacherId : wx.getStorageSync('userInfo').id ;
    util.request(api.TutorInfoByUserId, {
      userId: teacherId
    }, 'POST').then(function (res) {
      if (res.code == 200) {
        //判断是不是访问自己
        if (userId == res.result.tUser.id) {
          that.setData({
            canEdit: true
          })
        }
        var tutorEducationInfos = res.result.tutorEducationInfos[0]
        var label = `${tutorEducationInfos.schoolName} | ${tutorEducationInfos.specialName} | ${tutorEducationInfos.eduLevelName}`
        var name = res.result.tUser.fullName ? res.result.tUser.fullName : res.result.tUser.nickName
        let _temp = {
          name: name,
          share: res.result.descrition_NotFileter,
          avatarUrl: res.result.tUser.avatarUrl,
          label:label
        }
        that.setData({
          userInfo: _temp
        })
      } else {
        wx.showToast({
          title: '导师信息请求失败',
          icon: 'none'
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})